import HTMLParser
import string
import sys

TITLETAG = "title"
LINKTAG = "a"

class htmlParser(HTMLParser.HTMLParser):

    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.title = ""
        self.links = []
        self.body = ""
        self.title_flag = False
    
    def parse(self, data):
        """
        Feed the data and close the file. 
        """
        self.feed(data)
        self.close()
    
    def handle_starttag(self, tag, attributes):
        """
        Update object's links or change title_flag. 
        """
        if (tag == LINKTAG):
            self.links.append(attributes[0][1])
        if (tag == TITLETAG):
            self.title_flag = True
            
    def handle_endtag(self, tag):
        """
        Change title flag to False if the tag is a title tag. 
        """
        if (tag == TITLETAG):
            self.title_flag = False
            
    def handle_data(self, data):
        """
        Append data to the htmlparser's title or body. 
        """
        if self.title_flag:
            self.title += data
        else:
            self.body += data 